<?php

namespace app\api\controller;

use app\admin\service\ArticleService;
use app\common\helper\MyHelper;
use think\Controller;
use think\Db;

class Job extends Controller
{


        /** 循环标题或者内容特征
     * @return void
     */
    private function loopTitleOrContent($article, $ideFeaArr)
    {

        

        $title = $article['title'];
        $result = false;
        foreach ($ideFeaArr as $key => $value) {

            $result = $this->checkTitleOrContent($title, $article, $value);
//            var_dump($result);
            if ($result) {
                return true;
            }
        }
        return false;
    }


        /** 循环模版
     * @return void
     */
    function forTemplateArr($templateArr, $article)
    {
        
        //循环模版
        foreach ($templateArr as $key => $value) {

            if (empty($value['id'])) {
                $templateId = "";
            } else {
                $templateId = $value['id'];
            }

         
            $identifying_features = $value['identifying_features'];

            $ideFeaArr = json_decode($identifying_features, true);

            $resultTemplate = $this->loopTitleOrContent($article, $ideFeaArr);

            //匹配到模版 处理抽取文章内容
            if ($resultTemplate) {

                $is_kag = 0;
                if ($value['mode'] == 1) {
                    $is_kag = 2;
                }
                if ($value['mode'] == 2) {
                    $is_kag = 0;
                }

                if ($value['mode'] == 3) {
                    $is_kag = 3;
                }

                //匹配到模版
            
                return array("code" => 200, "template_id" => $templateId);

            }
        }
        // 没有命中模版
     
        return array("code" => 400, "template_id" => "");

    }


    /** 检查标题或者内容 行的且关系 都要命中
     * @return void
     */
    private function checkTitleOrContent($title, $article, $value)
    {



        $list = $value['value'];


        $content = $article['content'];
        foreach ($list as $key => $value) {

            $titleOrContent = $value['titleOrContent'];

            $reg = $value['value'];

            if ($titleOrContent == 1) {
                $result = $this->titleOrContentReg($title, $reg);

                if (!$result) {
                    return false;
                }
            } elseif ($titleOrContent == 2) {
                $result = $this->titleOrContentReg($content, $reg);
                if (!$result) {
                    return false;
                }
            } else {




                if (empty($article[$titleOrContent])) {
                    return false;
                }


                $result = $this->titleOrContentReg($article[$titleOrContent], $value['value']);
                if (!$result) {
                    return false;
                }


            }
        }
        return true;

    }

    private function titleOrContentReg($title, $reg)
    {

        $result = preg_match($reg, $title);
        if ($result === 1) {
            return true;
        } else {
            return false;
        }
    }



}